﻿using AgileConfig.Client.RegisterCenter;

namespace Service.Core.OpenFegin
{
    public class AgileConfigServiceDiscoveryService(IDiscoveryService discoveryService) : IServiceDiscoveryService
    {
        public async Task<Uri> GetInstancesAsync(string serviceName)
        {

            if (discoveryService == null)
            {
                throw new ArgumentNullException(nameof(discoveryService), "AgileConfig naming service cannot be null.");
            }
            var instance = discoveryService.RandomOne(serviceName);
            if (instance == null)
            {
                throw new InvalidOperationException($"AgileConfig No healthy instance found for service: {serviceName}");
            }
            return await Task.FromResult(new UriBuilder()
            {
                Scheme = "http",
                Host = instance.Ip,
                Port = (int)instance.Port
            }.Uri);

            throw new NotImplementedException();
        }
    }
}
